package com.mysiteforme.admin.task;
import com.mysiteforme.admin.MysiteformeApplication;
import com.mysiteforme.admin.entity.scrapVO.Stage;
import com.mysiteforme.admin.service.scrap.ScrapService;
import com.mysiteforme.admin.service.scrap.StageService;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

@Component("systemTask")
public class SystemTask {

   @Autowired
   JavaMailSenderImpl mailSender;
	 @Autowired
   StageService stageService;
    @Autowired
	  ScrapService ScrapService;
   private static Log log = LogFactory.get();

   public void synchronizationArticleView(String params,String emailGroup) throws AddressException {
	   emailGroup = emailGroup.replace(",",";");
       this.sendHtmlMail(emailGroup);
   }

   public void sendHtmlMail(String emailGroup) {
      try {
         Configuration e = new Configuration(Configuration.VERSION_2_3_0);
         ClassLoader loader = MysiteformeApplication.class.getClassLoader();
         e.setClassLoaderForTemplateLoading(loader, "ftl");
         Template template = e.getTemplate("mailtemplate.ftl");
         List<Stage> stages= stageService.ScrapStageDaily();
		   int input=0;
		   for (int i = 0; i < stages.size(); i++) {
			   if(stages.get(i).getScrapProject().equals("Input/投入数")) {
				    input = stages.get(i).getInput();
				   break;
			   }
		  }
		   Map<String, Object> model = new HashMap<>();
			 Map<Object, Object> queryMap = new HashMap<Object, Object>();  
	         if (stages.size()<=0 && ScrapService.byProcessScrapIndex(queryMap).size()<=0){
		           this.NotUpadata("luo.rober@qq.com", emailGroup, "通知-今日未更新", "今日未更新数据提醒");
		 	        return;
		        }
			 
	         // 每日Final inspection
	         if (stages.size()>0 ){
	        	 model.put("stagesNull",1);
		         model.put("input",input);
	        	model.put("stages",stageService.ScrapStageDaily());
	         }else {
	        	 model.put("stagesNull",0);
			}
	         
	         // 每日PE
	         if ( ScrapService.byProcessScrapIndex(queryMap).size()>0){
	        	  model.put("deatailNull",1);	
	              model.put("deatail",ScrapService.byProcessScrapIndex(queryMap));
	         }else {
		           model.put("deatailNull",0);	
		 	 }  
           String html = FreeMarkerTemplateUtils.processTemplateIntoString(template,model);
           LocalDateTime localDateTime = LocalDateTime.now();
           String dataTimeString =localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

         this.sendHtmlMail("luo.rober@qq.com", emailGroup, "Update the daily Scrap record ( "+dataTimeString+"）", html);
      } catch (Exception var6) {
         var6.printStackTrace();
      }

   }
	/*
	 * 邮箱模板发送器
	 */   
   public void sendHtmlMail(String from, String to,String subject, String content) {
      try {
         MimeMessage e = this.mailSender.createMimeMessage();
         MimeMessageHelper helper = new MimeMessageHelper(e, true);
         String toTmp= to;
        String[] toEmail = toTmp.split(";"); //目标必须字符串或数组，多接收人时必须为数组，用字府串会报异常
         helper.setTo(toEmail);
         helper.setFrom(from);
         helper.setSubject(subject);
        helper.setText(content, true);
         this.mailSender.send(e);
      } catch (MessagingException var7) {
         System.out.println("发送失败");
      }
   }
   
	/*
	 * 今日未更新信息
	 */   
   public void NotUpadata(String from, String to,String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(subject);
        message.setText(content);
         String[] to2 = to.split(";"); //目标必须字符串或数组，多接收人时必须为数组，用字府串会报异常
        message.setTo(to2); 
        message.setFrom(from);
      this.mailSender.send(message);
}
   
}
